package com.github.elliot.gatewaycenter.security.converter;


import com.alibaba.fastjson2.JSONObject;
import com.github.elliot.gatewaycenter.common.ResponseEnum;
import com.github.elliot.gatewaycenter.common.constant.AuthConstant;
import com.github.elliot.gatewaycenter.domain.AdminUserPO;
import com.github.elliot.gatewaycenter.security.jwt.JwtTokenGenerator;
import com.github.elliot.gatewaycenter.util.WebExchangeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.authentication.ServerFormLoginAuthenticationConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * 用户上传的信息转换为UsernamePasswordAuthenticationToken
 */
@Component
public class RefreshTokenAuthenticationConverter extends ServerFormLoginAuthenticationConverter {

    @Autowired
    private JwtTokenGenerator jwtTokenGenerator;

    @Override
    public Mono<Authentication> convert(ServerWebExchange exchange) {
        return WebExchangeUtils.parseRequestBodyToJson(exchange)
                .flatMap(jsonNode -> {
                    String refreshToken = jsonNode.has(AuthConstant.REFRESH_TOKEN) ?
                            jsonNode.get(AuthConstant.REFRESH_TOKEN).asText() : null;
                    // 创建Authentication对象
                    if (StringUtils.isNoneBlank(refreshToken)) {
                        JSONObject jsonObject = jwtTokenGenerator.decodeAndVerify(refreshToken);

                        Object userNoObj = jsonObject.get("userNo");
                        String userNo = Objects.nonNull(userNoObj) ? userNoObj.toString() : null;
                        Object usernameObj = jsonObject.get("username");
                        String username = Objects.nonNull(usernameObj) ? usernameObj.toString() : null;
                        Object mobileObj = jsonObject.get("mobile");
                        String mobile = Objects.nonNull(mobileObj) ? mobileObj.toString() : null;
                        Object mailObj = jsonObject.get("mail");
                        String mail = Objects.nonNull(mailObj) ? mailObj.toString() : null;

                        AdminUserPO adminUserPO = new AdminUserPO();
                        adminUserPO.setUserNo(userNo);
                        adminUserPO.setUsername(username);
                        adminUserPO.setMobile(mobile);
                        adminUserPO.setMail(mail);

                        return Mono.just(new UsernamePasswordAuthenticationToken(adminUserPO, null));
                    } else {
                        return Mono.error(new BadCredentialsException(ResponseEnum.LOGIN_FAILED.getMsg()));
                    }
                });

    }

}

